package com.example.demothree.controller;

import com.example.demothree.bean.Employee;
import com.example.demothree.bean.Managers;
import com.example.demothree.bean.UserName;
import com.example.demothree.service.LoginService;
import com.example.demothree.service.XtService;
import com.example.demothree.util.MyMD5Util;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("xt")
public class XtController {
    @Resource
    private XtService xtService;
    @Resource
    private LoginService loginService;
    private Managers managers;
    private UserName userName;

    private Long xtId = 0L;

    @RequestMapping("jbzl/{name}")
    public String jbzl(@PathVariable String name) {
        if (loginService.adminname(name) != null) {
            managers = loginService.adminname(name);
            return "forward:/xt/addglyzc/"+managers.getId();
        }else {
            Employee employee = loginService.loginname(name);
            System.out.println(name);
            return "forward:/login/addyh/"+employee.getName();
        }

    }
    //用户管理
    @RequestMapping("xtyhgl")
    public String xtyhgl(Model model,
                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                         @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.xtId != 0L){//判断是否删除成功
            Long deleteYhgl1 = this.xtId;
            model.addAttribute("deleteYhgl1",deleteYhgl1);
        }
        this.xtId = 0L;
        //分页
        Page<Employee> pageyhgl = xtService.xtYhgl(page,size);
        model.addAttribute("pageyhgl",pageyhgl);
        return "xtyhgl";
    }
    //删除用户
    @GetMapping("xtyhglDelete/{id}")
    public String xtyhglDelete(@PathVariable Long id) {
        //按id删除
        xtService.deletekhzl(Long.valueOf(id));
        //删除弹窗
        this.xtId = id;
        return "forward:/xt/xtyhgl";
    }
    //管理员管理
    @RequestMapping("xtglygl")
    public String xtglygl(Model model,
                          @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.xtId != 0L){//判断是否删除成功
            Long deleteGlygl1 = this.xtId;
            model.addAttribute("deleteGlygl1",deleteGlygl1);
        }
        this.xtId = 0L;
        //分页
        Page<Managers> pageglygl = xtService.xtglygl(page,size);
        model.addAttribute("pageglygl",pageglygl);
        return "xtglygl";
    }
    //删除管理员
    @GetMapping("xtglyDelete/{id}")
    public String xtglyDelete(@PathVariable Long id) {
        //按id删除
        xtService.deletegly(Long.valueOf(id));
        //删除弹窗
        this.xtId = id;
        return "redirect:/xt/xtglygl";
    }
    @RequestMapping("addgly")
    public String addgly(Model model) {
        if (this.xtId.equals(1L)) {
            model.addAttribute("managers1",managers);
        }else {
            int a = 1;
            System.out.println(a);
            model.addAttribute("a", a);
        }
        this.xtId = 0L;
        return "addgly";
    }
    @GetMapping("addglyzc/{id}")
    public String addglyzc(@PathVariable Long id) {
        if (id != null) {
            managers = loginService.findglybyId(Long.valueOf(id));
            this.xtId = 1L;
        }
        return "forward:/xt/addgly";
    }

    //添加管理员
    @RequestMapping("addglyzc")
    @ResponseBody
    public String addglyzc(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchAlgorithmException {
        String encryptedPwd = null;

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        //加密
        encryptedPwd = MyMD5Util.getEncryptedPwd(password);

//        Employee employee1 = loginService.findByName(name);
//        userName = loginService.findUserNameByName(name);
        userName = loginService.findmanagersId(Long.valueOf(id));
        int addgly = 0;
//        if (userName == null) {
            if (userName != null) {
                //修改
                managers = loginService.findglybyId(Long.valueOf(id));
            }else {
                //添加
                managers = new Managers();
            }
            managers.setName(name);
            managers.setAge(Long.valueOf(age));
            managers.setGender(gender);
            managers.setPhone(phone);
            managers.setPassword(encryptedPwd);
            try {
                if (userName != null) {
                    //修改管理员
                    loginService.upgly(managers, Long.valueOf(id));
                }else {
                    //添加管理员
                    loginService.addgly(managers);
                }
                addgly = 100;
            } catch (Exception e){
                addgly = 200;
                e.printStackTrace();
            }
//        }
        response.getWriter().print(addgly);
        return null;
    }
}
